
/**		
 * 
 *	uk.co.lmfm.guppy.view.components.workspace.WorkspaceView
 *	
 *	@version 1.00 | Jan 14, 2010
 *	@author Justin Windle
 *  
 **/
 
package uk.co.lmfm.guppy.view.components.workspace 
{
	import uk.co.lmfm.guppy.model.vo.WorkspaceVO;
	import uk.co.lmfm.guppy.view.components.events.PopUpViewEvent;
	import uk.co.lmfm.guppy.view.components.core.AbstractView;
	import uk.co.lmfm.guppy.view.components.core.IViewComponent;
	import uk.co.lmfm.guppy.view.components.events.WorkspaceBoxEvent;
	import uk.co.lmfm.guppy.view.components.events.WorkspaceViewEvent;

	import com.bit101.components.PushButton;

	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filesystem.File;

	/**
	 * WorkspaceView
	 */
	public class WorkspaceView extends AbstractView implements IViewComponent
	{
		//	----------------------------------------------------------------
		//	CONSTANTS
		//	----------------------------------------------------------------

		public static const BOX_GAP : int = 10;

		//	----------------------------------------------------------------
		//	PRIVATE MEMBERS
		//	----------------------------------------------------------------

		private var _boxes : Sprite = new Sprite();
		private var _activeBox : WorkspaceBox;
		private var _createPopUp : CreateWorkspacePopUp = new CreateWorkspacePopUp();
		private var _refreshButton : PushButton = new PushButton();		private var _newButton : PushButton = new PushButton();
		private var _clone : File = new File();

		//	----------------------------------------------------------------
		//	CONSTRUCTOR
		//	----------------------------------------------------------------

		public function WorkspaceView()
		{
			super();
			
			_newButton.label = "New Workspace";
			_newButton.addEventListener(MouseEvent.CLICK, onButtonPressed);
			
			_refreshButton.label = "Refresh List";
			_refreshButton.addEventListener(MouseEvent.CLICK, onButtonPressed);
			
			_createPopUp.addEventListener(PopUpViewEvent.CONFIRM, onCreateConfirmed);
			
			addChild(_boxes);
			addChild(_newButton);			addChild(_refreshButton);
			addChild(_createPopUp);
		}

		//	----------------------------------------------------------------
		//	PUBLIC METHODS
		//	----------------------------------------------------------------

		override protected function resize() : void
		{
			super.resize();
			
			var cols : int = (stage.stageWidth - BOX_GAP) / (WorkspaceBox.WIDTH + BOX_GAP);
			var box : WorkspaceBox;
			
			for (var i : int = 0;i < _boxes.numChildren;i++)
			{
				box = _boxes.getChildAt(i) as WorkspaceBox;
				
				box.x = BOX_GAP + ((i % cols) * (WorkspaceBox.WIDTH + BOX_GAP));				box.y = BOX_GAP + (((i / cols) << 0) * (WorkspaceBox.HEIGHT + BOX_GAP));
			}
			
			_newButton.x = stage.stageWidth - _newButton.width - 10;
			_newButton.y = stage.stageHeight - _newButton.height - 50;
			
			_refreshButton.x = _newButton.x - _newButton.width - 10;
			_refreshButton.y = _newButton.y;
		}

		public function clearWorkspaces() : void
		{
			while(_boxes.numChildren > 0)
			{
				var box : WorkspaceBox = _boxes.getChildAt(0) as WorkspaceBox;
				
				box.removeEventListener(WorkspaceBoxEvent.CLONE_BUTTON_PRESSED, onCloneButtonPressed);
				box.removeEventListener(WorkspaceBoxEvent.CONFIGURE_BUTTON_PRESSED, onConfigureButtonPressed);
				
				_boxes.removeChildAt(0);
				
				box = null;
			}
		}

		public function addWorkspace(workspace : WorkspaceVO) : void
		{
			var box : WorkspaceBox = new WorkspaceBox(workspace);
			box.addEventListener(WorkspaceBoxEvent.CLONE_BUTTON_PRESSED, onCloneButtonPressed);			box.addEventListener(WorkspaceBoxEvent.CONFIGURE_BUTTON_PRESSED, onConfigureButtonPressed);
			 
			_boxes.addChild(box);
			resize();
		}

		//	----------------------------------------------------------------
		//	EVENT HANDLERS
		//	----------------------------------------------------------------

		private function onButtonPressed(event : MouseEvent) : void
		{
			switch(event.currentTarget)
			{
				case _newButton :
				
					_createPopUp.show();
				
					break;
					
				case _refreshButton : 
				
					dispatchEvent(new WorkspaceViewEvent(WorkspaceViewEvent.REFRESH_WORKSPACES));
				
					break;
			}
		}

		private function onCreateConfirmed(event : PopUpViewEvent) : void
		{
			dispatchEvent(new WorkspaceViewEvent(WorkspaceViewEvent.CREATE_WORKSPACE, _createPopUp.workspace));
		}

		private function onCloneButtonPressed(event : WorkspaceBoxEvent) : void
		{
			_activeBox = event.currentTarget as WorkspaceBox;
			
			_clone.nativePath = _activeBox.workspace.location.parent.nativePath;
			_clone.addEventListener(Event.SELECT, onCloneDestinationSelected);
			_clone.browseForDirectory("Create a new directory to clone the '" + _activeBox.workspace.name + "' workspace to");
		}

		private function onConfigureButtonPressed(event : WorkspaceBoxEvent) : void
		{
			var box : WorkspaceBox = event.currentTarget as WorkspaceBox;
			dispatchEvent(new WorkspaceViewEvent(WorkspaceViewEvent.CONFIGURE_WORKSPACE, box.workspace.location));
		}

		private function onCloneDestinationSelected(event : Event) : void
		{
			_clone.removeEventListener(Event.SELECT, onCloneDestinationSelected);
			dispatchEvent(new WorkspaceViewEvent(WorkspaceViewEvent.CLONE_WORKSPACE, _activeBox.workspace.location, _clone));
		}
	}
}
